const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const dataBaseDir = 'E:/code/electron/dream/src/data/'

const db = low(new FileSync(`${dataBaseDir}/movie.json`))

const movies = db.read()
    .get('movies')
    .value()

let duration = 0
let size = 0

for (let movie of movies) {
    duration += movie.duration
    size += movie.size
}
console.log(duration, size)
console.log(formatSeconds(duration), formatBytes(size))


function formatSeconds(seconds) {
    // 四舍五入取整
    seconds = Math.round(seconds)
    // 计算小时、分钟和秒
    const hrs = Math.floor(seconds / 3600)
    const mins = Math.floor((seconds % 3600) / 60)
    const secs = seconds % 60

    // 将小时、分钟和秒格式化为字符串
    const h = hrs.toString().padStart(2, '0')
    const m = mins.toString().padStart(2, '0')
    const s = secs.toString().padStart(2, '0')

    // 组合成最终的时间字符串
    return `${h}:${m}:${s}`
}

function formatBytes(bytes, decimals = 2) {
    if (bytes === 0) return '0'

    const k = 1024
    const sizes = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']

    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return (
        parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + sizes[i]
    )
}
